//listflex
$('.listFlex').flexigrid
(
	{
		autoload: true,
		height: $('#main').height(),
		title: 'Employee List',
		url: root + module + '/listview',
		blockOpacity: 0,
		dataType: 'json',
		colModel : [
			{display: '', name : 'b.id', width : 40, align:'center', process: viewItem}
			,{display: 'Employee No.', name : 'b.employee_no', width : 66, align : 'left', sortable:true}
			,{display: 'Employee Name', name : 'b.fullname', width : 160, align : 'left', sortable:true}
			,{display: 'Status', name : 'c.es_name', width : 100, align : 'left', sortable:true}
			,{display: 'Available Leaves', name : 'available_leaves', width : 200, align : 'left', sortable:true}
			],		
		newp: 1,
		sortname: "b.fullname",
		sortorder: "asc",
		rp: 40,
		usepager: true,
		useRp: true,
		resizable: true,
		showTableToggleBtn: true,
		singleSelect: false
	}
);

$('.leavesFlex').flexigrid
(
	{
		autoload: true,
		height: $('#main').height(),
		title: 'Employee Leaves List',
		url: root + module + '/listviewemployeeleaves/' + $('input[name=item]').val(),
		blockOpacity: 0,
		dataType: 'json',
		colModel : [
			{display: '', name : 'a.employee_leave_id', width : 40, align:'center', process: viewItem}
			,{display: 'Leave Code', name : 'a.leave_code', width : 80, align : 'left', sortable:true}
			,{display: 'Leave Name', name : 'a.leave_name', width : 150, align : 'left', sortable:true}
			,{display: 'Available Days', name : 'a.available_days', width : 83, align : 'center', sortable:true, process: availableDays}
			],		
		buttons : [
			{name: 'Delete', bclass: 'delete', onpress : deleteItem},
			//{separator: true}
		],
		newp: 1,
		sortname: "b.leave_name",
		sortorder: "asc",
		rp: 40,
		usepager: true,
		useRp: true,
		resizable: true,
		showTableToggleBtn: true,
		singleSelect: false
	}
);

function availableDays(celDiv,id)
{
	var availableDays =celDiv.innerHTML;		
	celDiv.innerHTML = '<input id="'+id+'"  title="Available Days" value="'+availableDays+'" style="width:50px;height:20px;float:left;" class="validate[required]" /><a href="javascript:void(0)" class="go"></a>';
	
	$(celDiv).children('a').click(function(){
		var id = $(this).prev().attr('id');
		var val = $(this).prev().val();
		
		if(!isNaN(val))
		{
			if((val*1)>=0)
			{
				var action = root + module + '/updateItem';
				var dt = {item: id, available_days: val};
				showLoader('#main');
				$.ajax({
				   type: 'POST',
				   url: action,
				   data: dt,
				   dataType: 'json',
				   success: function(data){
						hideLoader();
						$('.leavesFlex').flexReload();
					},
				   error: function () { onError(); hideLoader(); }
				 });
			}
			else
				availableError();
		}
		else
			availableError();
	});
} 

function availableError()
{
	alert('Please enter a valid data.');
}


function deleteItem()
{
	if($('.leavesFlex .trSelected').length)
	{
		if(confirm('Are you sure you want to delete selected item/s?'))
		{
			showLoader('#main');
			var data = new Array();
			var count = 0;
			$('.leavesFlex .trSelected .fview').each(function(){
				data[count] = $(this).attr('id');
				count++;
			});			
			var action = root + module + '/deleteitem';
			var dt = {items: data};

			$.ajax({
			   type: 'POST',
			   url: action,
			   data: dt,
			   dataType: 'json',
			   success: function(data){
					// hideLoader();
					// $('.leavesFlex').flexReload();
					refreshLeaveDropDown();
				},
			   error: function () { onError(); hideLoader(); }
			 });
		}
	}
	else
	{
		alert("Please select item/s to delete.");
	}
}


$('#add-leave').click(function(){

	var action = root + module + '/additem';
	var dt = {user_id:$('input[name=item]').val(), leave_id: $('#available_leaves').val()};
	if($('#available_leaves').val()!="0")
	{
		$.ajax({
		   type: 'POST',
		   url: action,
		   data: dt,
		   dataType: 'json',
		   success: function(data){
				// hideLoader();
				// $('.leavesFlex').flexReload();
				refreshLeaveDropDown();
			},
		   error: function () { onError(); hideLoader(); }
		 });
	}
	else
		alert('There are no available leaves to be added.');

});

function refreshLeaveDropDown()
{
	var action = root + module + '/getAvailableLeaves';
	var dt = {user_id:$('input[name=item]').val()};

	$.ajax({
	   type: 'POST',
	   url: action,
	   data: dt,
	   dataType: 'json',
	   success: function(data){
			$('#available_leaves > option').remove();			
			$.each( data, function(i, n){			
				$('#available_leaves').append($('<option></option>').val(n.leave_id).html(n.leave_name + ' - '+ n.leave_code));
			});
			if(!data.length)
				$('#available_leaves').append($('<option></option>').val(0).html("No Available Leaves"));
				
			$('#available_leaves').val($('#available_leaves option:first').val());
			hideLoader();
			$('.leavesFlex').flexReload();
		},
	   error: function () { onError(); hideLoader(); }
	 });
}